{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets,preprocessing\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import r2_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "iris = load_iris()\n",
    "#定义数据、标签\n",
    "X = iris.data\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test,y_train,y_test = train_test_split(\n",
    "X,\n",
    "y,\n",
    "random_state=12,\n",
    "stratify=y,\n",
    "test_size=0.3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.00614742, -0.15874243,  0.38865271,  0.27908984],\n",
       "       [ 1.39550422,  0.27988797,  1.14544437,  1.48338159],\n",
       "       [-1.84913579, -0.37805763, -1.35778957, -1.32663251],\n",
       "       [ 1.13593302, -0.15874243,  0.73794117,  0.68052042],\n",
       "       [ 0.22743381, -0.81668802,  0.79615591,  0.54671023],\n",
       "       [-1.71935019, -1.69394882, -1.41600431, -1.19282231],\n",
       "       [-1.84913579, -0.15874243, -1.41600431, -1.32663251],\n",
       "       [ 0.48700502, -1.91326402,  0.44686745,  0.41290003],\n",
       "       [ 1.78486102,  0.27988797,  1.3200886 ,  0.81433062],\n",
       "       [-0.03213739, -0.81668802,  0.79615591,  0.94814081],\n",
       "       [-0.16192299, -0.15874243,  0.27222323,  0.01146945],\n",
       "       [ 0.61679062, -0.81668802,  0.67972643,  0.81433062],\n",
       "       [ 1.13593302,  0.06057277,  0.56329694,  0.41290003],\n",
       "       [-0.94063659, -1.25531842, -0.42635369, -0.12234075],\n",
       "       [-0.94063659,  0.71851836, -1.29957483, -1.32663251],\n",
       "       [-0.68106539,  1.37646396, -1.29957483, -1.32663251],\n",
       "       [ 2.43378902,  1.59577916,  1.72759179,  1.3495714 ],\n",
       "       [-0.29170859, -0.15874243,  0.21400848,  0.14527964],\n",
       "       [ 1.13593302,  0.49920317,  1.14544437,  1.75100198],\n",
       "       [-0.16192299, -1.25531842,  0.73794117,  1.08195101],\n",
       "       [ 0.61679062, -1.25531842,  0.73794117,  0.94814081],\n",
       "       [-0.16192299, -0.59737283,  0.21400848,  0.14527964],\n",
       "       [-0.55127979,  1.81509436, -1.41600431, -1.05901212],\n",
       "       [-1.07042219,  0.27988797, -1.47421906, -1.32663251],\n",
       "       [-1.20020779, -1.47463362, -0.25170946, -0.25615095],\n",
       "       [-0.42149419,  2.47303995, -1.35778957, -1.32663251],\n",
       "       [-0.94063659,  1.37646396, -1.29957483, -1.05901212],\n",
       "       [ 1.39550422,  0.06057277,  0.79615591,  1.48338159],\n",
       "       [ 0.09764821, -0.15874243,  0.79615591,  0.81433062],\n",
       "       [-1.20020779,  1.15714876, -1.35778957, -1.4604427 ],\n",
       "       [ 0.74657622, -0.81668802,  0.9125854 ,  0.94814081],\n",
       "       [-1.20020779, -0.15874243, -1.35778957, -1.32663251],\n",
       "       [ 1.52528982,  0.27988797,  0.56329694,  0.27908984],\n",
       "       [ 0.22743381, -1.91326402,  0.73794117,  0.41290003],\n",
       "       [-1.84913579,  0.27988797, -1.41600431, -1.32663251],\n",
       "       [ 2.43378902, -1.03600322,  1.84402128,  1.48338159],\n",
       "       [-0.42149419,  0.93783356, -1.41600431, -1.32663251],\n",
       "       [ 0.35721941, -1.03600322,  1.08722962,  0.27908984],\n",
       "       [-1.07042219,  0.93783356, -1.24136009, -0.79139173],\n",
       "       [-0.29170859, -0.59737283,  0.67972643,  1.08195101],\n",
       "       [-0.81085099,  2.25372475, -1.29957483, -1.4604427 ],\n",
       "       [-0.16192299,  1.59577916, -1.18314534, -1.19282231],\n",
       "       [-0.16192299, -0.59737283,  0.44686745,  0.14527964],\n",
       "       [-0.16192299, -1.03600322, -0.13527997, -0.25615095],\n",
       "       [ 0.74657622,  0.06057277,  1.02901488,  0.81433062],\n",
       "       [-1.07042219,  0.71851836, -1.29957483, -1.32663251],\n",
       "       [-0.94063659,  0.93783356, -1.35778957, -1.19282231],\n",
       "       [ 0.35721941, -0.59737283,  0.56329694,  0.01146945],\n",
       "       [ 0.74657622,  0.27988797,  0.9125854 ,  1.48338159],\n",
       "       [ 1.13593302,  0.06057277,  1.08722962,  1.61719179],\n",
       "       [-1.07042219,  1.15714876, -1.35778957, -1.32663251],\n",
       "       [-0.29170859, -1.25531842,  0.097579  , -0.12234075],\n",
       "       [-1.07042219,  0.71851836, -1.24136009, -1.05901212],\n",
       "       [-0.16192299,  2.91167035, -1.29957483, -1.05901212],\n",
       "       [ 1.00614742, -0.37805763,  0.5050822 ,  0.14527964],\n",
       "       [-0.42149419, -1.03600322,  0.38865271,  0.01146945],\n",
       "       [-0.94063659,  1.59577916, -1.24136009, -1.32663251],\n",
       "       [ 0.35721941, -0.37805763,  0.56329694,  0.27908984],\n",
       "       [-0.29170859, -0.37805763, -0.07706523,  0.14527964],\n",
       "       [-0.16192299, -0.37805763,  0.27222323,  0.14527964],\n",
       "       [ 0.74657622,  0.27988797,  0.44686745,  0.41290003],\n",
       "       [ 0.61679062,  0.49920317,  1.3200886 ,  1.75100198],\n",
       "       [ 1.26571862, -0.59737283,  0.62151168,  0.27908984],\n",
       "       [ 1.13593302,  0.06057277,  0.38865271,  0.27908984],\n",
       "       [ 1.13593302, -1.25531842,  1.20365911,  0.81433062],\n",
       "       [ 0.22743381,  0.71851836,  0.44686745,  0.54671023],\n",
       "       [ 2.69336023,  1.59577916,  1.55294757,  1.08195101],\n",
       "       [-0.03213739, -0.59737283,  0.79615591,  1.61719179],\n",
       "       [-1.32999339,  0.06057277, -1.24136009, -1.32663251],\n",
       "       [ 0.87636182, -0.15874243,  1.02901488,  0.81433062],\n",
       "       [-1.32999339,  0.71851836, -1.06671586, -1.32663251],\n",
       "       [ 1.13593302, -0.15874243,  0.85437065,  1.48338159],\n",
       "       [-0.81085099, -0.81668802,  0.097579  ,  0.27908984],\n",
       "       [-1.45977899,  0.27988797, -1.24136009, -1.32663251],\n",
       "       [ 1.26571862,  0.27988797,  1.26187385,  1.48338159],\n",
       "       [ 0.22743381, -0.15874243,  0.62151168,  0.81433062],\n",
       "       [-0.42149419, -1.25531842,  0.15579374,  0.14527964],\n",
       "       [ 1.13593302,  0.49920317,  1.14544437,  1.2157612 ],\n",
       "       [-0.55127979,  0.71851836, -1.29957483, -1.05901212],\n",
       "       [-1.58956459,  0.27988797, -1.35778957, -1.32663251],\n",
       "       [ 0.61679062, -1.25531842,  0.67972643,  0.41290003],\n",
       "       [ 0.35721941, -0.59737283,  0.15579374,  0.14527964],\n",
       "       [ 1.39550422,  0.06057277,  0.97080014,  1.2157612 ],\n",
       "       [ 0.74657622, -0.59737283,  1.08722962,  1.3495714 ],\n",
       "       [-0.94063659,  1.59577916, -1.29957483, -1.19282231],\n",
       "       [ 1.78486102, -0.15874243,  1.20365911,  0.54671023],\n",
       "       [-0.42149419, -1.47463362,  0.03936425, -0.12234075],\n",
       "       [-0.94063659,  0.49920317, -1.18314534, -0.92520192],\n",
       "       [ 0.61679062, -0.59737283,  0.79615591,  0.41290003],\n",
       "       [-0.29170859, -0.15874243,  0.44686745,  0.41290003],\n",
       "       [ 0.22743381, -1.91326402,  0.15579374, -0.25615095],\n",
       "       [-1.07042219,  0.93783356, -1.41600431, -1.19282231],\n",
       "       [-0.81085099,  0.71851836, -1.35778957, -1.32663251],\n",
       "       [-0.03213739,  2.03440955, -1.47421906, -1.32663251],\n",
       "       [-0.55127979, -0.15874243,  0.44686745,  0.41290003],\n",
       "       [-1.20020779, -1.25531842,  0.44686745,  0.68052042],\n",
       "       [ 0.61679062, -0.37805763,  1.08722962,  0.81433062],\n",
       "       [ 0.48700502, -0.59737283,  0.62151168,  0.81433062],\n",
       "       [-0.94063659,  1.59577916, -1.06671586, -1.05901212],\n",
       "       [-0.03213739, -0.81668802,  0.21400848, -0.25615095],\n",
       "       [ 0.74657622, -0.37805763,  0.33043797,  0.14527964],\n",
       "       [-0.55127979,  0.71851836, -1.18314534, -1.32663251],\n",
       "       [-1.20020779,  0.06057277, -1.29957483, -1.4604427 ],\n",
       "       [ 0.48700502,  0.71851836,  0.97080014,  1.48338159],\n",
       "       [-0.42149419, -1.47463362, -0.01885049, -0.25615095]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "#构建转换需实例\n",
    "scaler = StandardScaler( )\n",
    "#拟合及转换\n",
    "scaler.fit_transform(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#从线性模型库导入线性回归模型\n",
    "from sklearn.linear_model import LinearRegression\n",
    "# 构建模型实例\n",
    "lr = LinearRegression(normalize=True )\n",
    "# 训练模型\n",
    "lr.fit(X_train,y_train)\n",
    "# 作出预测\n",
    "y_pred = lr.predict(X_test )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#从树模型库导入决案树\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "#定义模型\n",
    "clf = DecisionTreeClassifier(max_depth=5 )\n",
    "#训练模型\n",
    "clf.fit(X_train,y_train )\n",
    "#使用决策树分类算法解决二分类问题，得到的是类别\n",
    "y_pred = clf.predict(X_test )\n",
    "#y_prob 为每个样本预测为\"62和人所？类的概率\n",
    "y_prob = clf.predict_proba(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 2, 0, 1, 1, 2, 1, 0, 1, 1, 1, 1, 2, 0, 2, 2, 2, 2, 0, 0,\n",
       "       0, 0, 1, 2, 2, 1, 1, 2, 2, 2, 2, 0, 0, 1, 1, 1, 2, 2, 0, 1, 0, 2,\n",
       "       0])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#从聚类模型库导入kmeans\n",
    "from sklearn.cluster import KMeans\n",
    "#构建生类实例\n",
    "kmeans = KMeans(n_clusters=3,random_state=0)\n",
    "#拟合\n",
    "kmeans.fit(X_train)\n",
    "#预测\n",
    "kmeans.predict(X_test )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.92461872 0.05306648 0.01710261]\n",
      "[4.22824171 0.24267075 0.0782095 ]\n"
     ]
    }
   ],
   "source": [
    "#导入PC4库\n",
    "from sklearn.decomposition import PCA\n",
    "#设置主成分数量为3，m_components代表主成分数量\n",
    "pca = PCA(n_components=3)\n",
    "#训练模型\n",
    "pca.fit(X)\n",
    "#投影后各个特征维度的方差比例(这里是三个主成分)\n",
    "print(pca.explained_variance_ratio_)\n",
    "#投影后的特征维度的方差\n",
    "print(pca.explained_variance_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9777777777777777"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#从评价指标库导入准确率\n",
    "from sklearn.metrics import accuracy_score\n",
    "#计算样本的准确率\n",
    "accuracy_score(y_test,y_pred )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "#从模型选择库导入交叉验证分数\n",
    "from sklearn.model_selection import cross_val_score\n",
    "clf = DecisionTreeClassifier(max_depth=5 )\n",
    "#使用5折区叉验证对决策树模型进行评估，使用的评分函数为FI值\n",
    "scores = cross_val_score(clf,X_train,y_train,cv=5,scoring= 'f1_weighted')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 1, 'kernel': 'linear'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#从模型选择库导入网格搜索\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn import svm\n",
    "svc = svm.SVC( )\n",
    "#把超参数集合作为字典\n",
    "params = { 'kernel' : [ 'linear' , 'rbf' ], 'C' : [1,10]}\n",
    "#进行网格搜索，使用了文持问量机分类器，并进行五折区义验证\n",
    "grid_search = GridSearchCV(svc,params,cv=5)\n",
    "#模型训练\n",
    "grid_search.fit(X_train,y_train)\n",
    "#获取模型最优超参数组合\n",
    "grid_search.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'C': 2, 'kernel': 'linear'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#从模型选择库导入随机搜索\n",
    "from sklearn.model_selection import RandomizedSearchCV\n",
    "from scipy.stats import randint\n",
    "svc = svm.SVC( )\n",
    "#把超参数集合作为字典\n",
    "param_dist = { 'kernel' : [ 'linear' , 'rbf' ], 'C' :randint(1,20)}\n",
    "#进行随机搜索\n",
    "random_search = RandomizedSearchCV(svc,param_dist,n_iter=10)\n",
    "#模型训练\n",
    "random_search.fit(X_train,y_train )\n",
    "#获取最优超参数组合\n",
    "random_search.best_params_"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
